<script setup>
import { ref, onMounted, inject } from 'vue';
import { ElMessage } from 'element-plus';
import { Delete, Search, Document } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
import { BREADCRUMBS } from '@/plugins/global';

const axios = inject('axios'), router = useRouter();

// ---- 初始化。
onMounted(() => {
  BREADCRUMBS.splice(0, BREADCRUMBS.length,
    { "label": '角色列表', "path": '/home/role_list' }
  );
  roleQuery();
});

// ---- 角色查询。
const roles = ref([]), roleTotal = ref(null), roleCondition = ref({
  "page": 1,
  "num": 10,
  "roleNameFuzzy": null
});
const roleQuery = () => {
  axios.get('/api/cms-web/role/queries', {
    params: {
      "page": roleCondition.value.page,
      "num": roleCondition.value.num,
      "total": true,
      "roleNameFuzzy": roleCondition.value.roleNameFuzzy
    }
  }).then(response => {
    if (response.data.success) {
      roles.value = response.data.data.data;
      roleTotal.value = response.data.data.total;
    } else {
      ElMessage.error(response.data.message);
    }
  });
}

// ---- 角色删除准备。
const roleDeleteDialogModel = ref(false), roleDelete = ref(null);
const roleDeletePrepare = (role) => {
  roleDeleteDialogModel.value = true
  roleDelete.value = role;
}

// ---- 角色删除确认。
const roleDeleteConfirm = () => {
  axios.delete(`/api/cms-web/role/delete?code=${roleDelete.value.code}`).then(response => {
    if (response.data.success) {
      ElMessage.success('删除成功！');
      roleDeleteDialogModel.value = false;
      roleQuery();
    } else {
      ElMessage.error(response.data.message);
    }
  });
}
</script>

<template>
  <div class="role-list-page-div-class">
    <!-- 角色查询域 -->
    <div style="width: 100%">
      <el-form :inline="true" :model="roleCondition">
        <el-form-item label="名称">
          <el-input v-model="roleCondition.roleNameFuzzy" :prefix-icon="Search" clearable style="width: 200px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="roleQuery()">查询</el-button>
          <el-button @click="router.push('/home/role_upsert')">插入</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="role-query-table-div-class">
      <el-table :data="roles" stripe style="width: 100%">
        <el-table-column prop="code" label="代码" align="center" />
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column label="操作" fixed="right" align="center">
          <template #default="scope">
            <el-button :icon="Document" circle @click="router.push(`/home/role_upsert/${scope.row.code}`)" />
            <el-button type="danger" :icon="Delete" circle @click="roleDeletePrepare(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="role-query-pagination-div-class">
      <el-pagination background layout="total, prev, pager, next, sizes" size="small" :page-sizes="[10, 20, 50, 100]"
        :total="roleTotal" v-model:page-size="roleCondition.size" v-model:current-page="roleCondition.page"
        @upsert:page-size="roleQuery()" @upsert:current-page="roleQuery()" />
    </div>
  </div>
  <!-- 角色删除域 -->
  <el-dialog header="确认删除" v-model="roleDeleteDialogModel" width="30%">
    <span>您确定要删除角色[{{ roleDelete.name }}]吗？这将导致持有该角色的员工无法执行相应操作！</span>
    <template #footer>
      <span>
        <el-button @click="roleDeleteDialogModel = false">取消</el-button>
        <el-button type="primary" @click="roleDeleteConfirm()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped>
.role-list-page-div-class {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 15px;
  width: 100%;
  height: 100%;
  padding: 20px 20px 20px 20px;
}

.role-query-table-div-class {
  flex: 1;
  width: 100%;
  overflow: hidden;
}

.role-query-pagination-div-class {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: flex-end;
  width: 100%;
}
</style>
